﻿using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tippelde.Entities
{
    public class TippeldeEntities : IdentityDbContext<User>
    {

        public DbSet<Bet> Bets { get; set; }
        public DbSet<BetResult> BetResults { get; set; }
        public DbSet<Event> Events { get; set; }
        public DbSet<EventDescriptor> EventDescriptors { get; set; }
        public DbSet<Group> Groups { get; set; }
        public DbSet<Result> Results { get; set; }
        public DbSet<Team> Teams { get; set; }
        public DbSet<Log> Logs { get; set; }
        public DbSet<UserGroup> UserGroups { get; set; }
        public DbSet<News> News { get; set; }

        public TippeldeEntities()
            : base("DefaultConnection")
        {
        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<User>().ToTable("Users");
            modelBuilder.Entity<User>().HasMany<UserGroup>(u => u.UserGroups).WithRequired(g => g.User);
            
            modelBuilder.Entity<User>().HasMany<Bet>(u => u.Bets).WithRequired(b => b.User);
            modelBuilder.Entity<User>().HasMany<BetResult>(u => u.BetResults).WithRequired(b => b.User);

            modelBuilder.Entity<Group>().HasMany<UserGroup>(g => g.UserGroups).WithRequired(g => g.Group);
            modelBuilder.Entity<Group>().Property(g => g.Name).IsRequired();
            modelBuilder.Entity<Group>().Property(g => g.ShortName).IsRequired();

            modelBuilder.Entity<Event>().HasRequired<EventDescriptor>(e => e.EventDescriptor).WithRequiredPrincipal(ed => ed.Event);
            modelBuilder.Entity<Event>().HasOptional<Result>(e => e.Result).WithRequired(r => r.Event);
            modelBuilder.Entity<Event>().Property(p => p.Name).IsRequired();
            modelBuilder.Entity<Event>().Property(p => p.ShortName).IsRequired();

            modelBuilder.Entity<Team>().Property(g => g.Name).IsRequired();
            modelBuilder.Entity<Team>().Property(g => g.ShortName).IsRequired().HasMaxLength(6);
            modelBuilder.Entity<Team>().Property(g => g.LogoUrl).IsRequired();

            modelBuilder.Entity<BetResult>().HasRequired<Bet>(br  => br.Bet).WithOptional(b => b.BetResult);

            modelBuilder.Entity<News>().Property(n => n.Title).IsRequired();
            modelBuilder.Entity<News>().Property(n => n.Content).IsRequired();
            modelBuilder.Entity<News>().Property(n => n.ContentShort).IsRequired();
            modelBuilder.Entity<News>().Property(n => n.SeoUrl).IsRequired();
            modelBuilder.Entity<News>().Property(n => n.AddedAt).IsRequired();
            modelBuilder.Entity<News>().HasMany<Group>(n => n.Groups).WithMany(g => g.News)
                .Map(m => m.MapLeftKey("GroupId").MapRightKey("NewsId").ToTable("NewsGroups"));

            modelBuilder.Entity<IdentityUser>().ToTable("Users");

            modelBuilder.Entity<IdentityRole>().ToTable("Roles");
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
            //insert relation here to join the two tables

        }
    }
}

